﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using Microsoft.IdentityModel.Protocols.WSTrust;
using System.IdentityModel.Tokens;
using Microsoft.IdentityModel.Claims;
using System.Threading;

namespace paris.veracruz
{
    [ServiceContract(ConfigurationName = "DuderContract")]
    public interface IDuder
    {
        [OperationContract(Name = "GetFriends")]
        string[] GetFriends();
    }

    public partial class _Default : System.Web.UI.Page
    {
        #region consts

        public const string CachedToken = "CachedToken";

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void FederatedPassiveSignIn1_SignInError(object sender, Microsoft.IdentityModel.Web.Controls.ErrorEventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IDuder> l_channelFactory = new ChannelFactory<IDuder>("DuderService");
            FederatedClientCredentials.ConfigureChannelFactory<IDuder>(l_channelFactory);
            SecurityToken de = (SecurityToken)Page.Session[CachedToken];
            IDuder l_channel = l_channelFactory.CreateChannelActingAs((SecurityToken)Page.Session[CachedToken]);
            string[] l_friendNames = l_channel.GetFriends();

            IClaimsIdentity l_identity = ((IClaimsPrincipal)Thread.CurrentPrincipal).Identity as IClaimsIdentity;
            NameLabel.Text = l_identity.Name;
            FriendsListBox.DataSource = l_friendNames;
            FriendsListBox.DataBind();
        }

        protected void FederatedPassiveSignIn1_SignedIn(object sender, EventArgs e)
        {
           
        }

        protected void FederatedPassiveSignIn1_SecurityTokenReceived(object sender, Microsoft.IdentityModel.Web.SecurityTokenReceivedEventArgs e)
        {
            //Session[CachedToken] = e.SecurityToken;
        }

        protected void FederatedPassiveSignIn1_SecurityTokenValidated(object sender, Microsoft.IdentityModel.Web.SecurityTokenValidatedEventArgs e)
        {
            
        }

        protected void FederatedPassiveSignIn1_SessionSecurityTokenCreated(object sender, Microsoft.IdentityModel.Web.SessionSecurityTokenCreatedEventArgs e)
        {
            Session[CachedToken] = e.SessionToken.BootstrapTokens[0];


        }
    }
}
